package com.example.demo.config;


import com.example.demo.API.TestAPI;
import com.example.demo.API.GameAPI;
import com.example.demo.API.MatchController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;


/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2023-10-25
 * Time:22:24
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private TestAPI testAPI;

    @Autowired
    private MatchController matchController;

    @Autowired
    private GameAPI gameController;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(testAPI, "/test");
        webSocketHandlerRegistry.addHandler(matchController,"/findMatch")
                //在注册 websocket API 的时候，就需要把前面准备好的 HttpSession 给搞过来，(搞到 Websocket 的Session 中)
                //用户登录就会给 HttpSession 中保存用户的信息
                .addInterceptors(new HttpSessionHandshakeInterceptor());
        webSocketHandlerRegistry.addHandler(matchController,"/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor());

    }



}
